﻿using System.Collections;
using System.Web;
using System;
using System.IO;
using ASPNETVN.PORTAL.Components.Configuration;

namespace ASPNETVN.PORTAL.Modules.FileExpress.Components
{
    public class FileExpressUpload : IHttpHandler
    {
        #region IHttpHandler Members

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            if (context.Request.Files.Count > 0)
            {
                int mid = Convert.ToInt32(context.Request.QueryString["mid"]);
                Hashtable ht = PortalConfiguration.GetModuleSettings(mid);
                string uploadPath = ht["UploadPath"].ToString();


                // get the applications path
                string tempFile = context.Request.PhysicalApplicationPath;
                // loop through all the uploaded files
                for (int j = 0; j < context.Request.Files.Count; j++)
                {
                    // get the current file
                    HttpPostedFile uploadFile = context.Request.Files[j];
                    // if there was a file uploded
                    if (uploadFile.ContentLength > 0)
                    {
                        // Tao thu muc de upload, neu khong co thi tao moi.
                        string physicalDirectory = string.Format("{0}{1}", tempFile, uploadPath + "\\FileExpress\\" + context.Request.QueryString["mid"]);
                        if (Directory.Exists(physicalDirectory) == false)
                        {
                            Directory.CreateDirectory(physicalDirectory);
                        }


                        string physicalPath = physicalDirectory + "\\" + uploadFile.FileName;
                        int i = 1;
                        while (File.Exists(physicalPath))
                        {
                            physicalPath = physicalDirectory + "\\" + i + "_" + uploadFile.FileName;
                            i++;
                        }
                        // Lưu file.
                        uploadFile.SaveAs(physicalPath);

                        int itemID = 0;
                        if (context.Request.QueryString["ItemID"] != null)
                        {
                            try
                            {
                                itemID = Convert.ToInt32(context.Request.QueryString["ItemID"]);                                
                            }
                            catch
                            {
                                itemID = 0;
                            }
                        }

                        // Cập nhật CSDL.
                        if (itemID > 0)
                        {
                            Attachment attachment = new Attachment();
                            attachment.PackageID = itemID;
                            attachment.TotalDownloads = 0;
                            attachment.OriginalFileName = uploadFile.FileName;
                            attachment.FileName = physicalPath.Substring(physicalPath.LastIndexOf('\\') + 1);
                            attachment.FileSize = uploadFile.ContentLength;
                            string ext = uploadFile.FileName.Substring(uploadFile.FileName.LastIndexOf('.') + 1);
                            attachment.FileType = ext;
                            attachment.Url = Global.ApplicationPath + "/" + uploadPath + "/FileExpress/" + context.Request.QueryString["mid"] + "/" + attachment.FileName;
                            attachment.Insert();
                        }
                    }
                }
            }
            HttpContext.Current.Response.Write(" ");
        }

        #endregion
    }
}